<?php
/**
 * 系统门店经营范围管理
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-03-28
 * Time: 下午 4:28
 */
namespace App;

use Model\StoreManageMod;

class StoreManage extends Action
{
    private $StoreManage = null;   //门店经营范围
	public function __construct()
	{
		parent::__construct();
        $this->StoreManage = new StoreManageMod();
	}

    /**
     * 获取门店的所有经营范围
     */
    public function findAll()
    {
        $_where = array('uniacid'=>$this->_uniacid,'status'=>1);
        if(isset($this->_gpc['status'])){
            $_where['status'] = $this->_gpc['status'];
        }
        $_list = $this->StoreManage->findAll($_where,array('order'=>array('id','desc')),array('id','name'));
        if(count($_list) > 0){
            foreach($_list as $_k=>$_v){
                $_list[$_k]['selected'] = 0;
            }
            $this->_result = array('status'=>'success','data'=>$_list);
        }
        $this->result($this->_result);
    }

    /**
     * 搜索经营范围，如果系统没有则新增后在返回
     * @param keyword 搜索的关键词
     */
    public function search()
    {
        $_where = array('uniacid'=>$this->_uniacid);
        $_where['name like'] = '%'.$this->_gpc['keyword'].'%';
        $_where['status'] = 1;
        $_list = $this->StoreManage->findAll($_where,array('order'=>array('id','asc')),array('id','name'));
        if(count($_list)){
            foreach($_list as $_k=>$_v){
                $_list[$_k]['selected'] = 0;
            }
            $this->_result = array('status'=>'success','data'=>$_list);
        }else{
            $_store_manage = $this->StoreManage->findOne(array('uniacid'=>$this->_uniacid,'name'=>$this->_gpc['keyword']),array('id','status'));
            if($_store_manage){
                if($_store_manage['status'] == 9){
                    $this->StoreManage->update(array('status'=>2),array('id'=>$_store_manage['id']));
                }
            }else{
                $_add = array(
                    'uniacid'=>$this->_uniacid,
                    'name'=>$this->_gpc['keyword']
                );
                $this->StoreManage->insert($_add);
            }
        }
        $this->result($this->_result);
    }

    /**
     * 新增
     * @param name  名称
     */
    public function add()
    {
        $_data = array('uniacid'=>$this->_uniacid,'name'=>$this->_gpc['name']);
        $_store_manage = $this->StoreManage->findOne($_data,array('id','status'));
        if($_store_manage && $_store_manage['status'] != 9){
             $this->_result = array('status'=>'error','err_msg'=>'经营类型已存在','err_status'=>9);
        }else{
            if(!$_store_manage){
                $_data['status'] = 1;
                if($this->StoreManage->insert($_data)){
                    $this->_result = array('status'=>'success');
                }else{
                    $this->_result = array('status'=>'error','err_msg'=>'添加失败','err_status'=>8);
                }
            }else{
                if($this->StoreManage->update(array('status'=>1),array('id'=>$_store_manage['id']))){
                    $this->_result = array('status'=>'success');
                }else{
                    $this->_result = array('status'=>'error','err_msg'=>'添加失败','err_status'=>8);
                }
            }
        }
        $this->result($this->_result);
    }

    /**
     * 修改经营范围状态
     * @param status  改变的状态
     * @param manage_id 经营范围id
     */
    public function changeStatus()
    {
        if($this->StoreManage->update(array('status'=>$this->_gpc['status']),array('id'=>$this->_gpc['manage_id']))){
            $this->_result = array('status'=>'success');
        }
        $this->result($this->_result);
    }


}